﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using System.Globalization;

public partial class time : System.Web.UI.Page
{
    //inject shiftBLL
    private IDayNightBLL dayNightBLL = Factory<IDayNightBLL>.CreateInstance();
    //Inject log4net
    private static readonly ILog logger = LogManager.GetLogger(typeof(time).Name);
    protected void Page_Load(object sender, EventArgs e)
    {
       
            Inits();
       
    }

    #region Private methods
    //Init page
    private void Inits()
    {
        try
        {
            List<DayNight> list= dayNightBLL.GetAll();
            if (list == null)
                list = DayNightWhenNull();
            rptDayNight.DataSource = list;
            rptDayNight.DataBind();
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }

    /// <summary>
    /// DayNightWhenNull
    /// </summary>
    /// <returns></returns>
    private List<DayNight> DayNightWhenNull()
    {
        List<DayNight> list = new List<DayNight>();
        DayNight dayNight;
        dayNight = new DayNight();
        dayNight.Id = false;
        dayNight.Name = "Ngày";
        dayNight.FromTime = new DateTime(9999, 1, 1, 0, 0, 0);
        dayNight.ToTime = new DateTime(9999, 1, 1, 22, 0, 0);
        list.Add(dayNight);
        dayNight = new DayNight();
        dayNight.Id = true;
        dayNight.Name = "Đêm";
        dayNight.FromTime = new DateTime(9999, 1, 1, 22, 0, 0);
        dayNight.ToTime = new DateTime(9999, 1, 1, 6, 0, 0);
        list.Add(dayNight);

        return list;
    }

    ///// <summary>
    ///// Save data
    ///// </summary>
    private void Save()
    {
        try
        {
            //init BO and list
            DayNight dayNight = null;
            List<DayNight> list = new List<DayNight>();
            Employee emp = Utils.GetEmployeeSession();

            foreach (RepeaterItem item in rptDayNight.Items)
            {
                //find controls
                TextBox txtFrom = (TextBox)item.FindControl("txtFrom");
                TextBox txtTo = (TextBox)item.FindControl("txtTo");
                HiddenField hdnId = (HiddenField)item.FindControl("hdnId");
                Label lblTime = (Label)item.FindControl("lblTime");

                dayNight = new DayNight();

                //set to BO
                dayNight.Id = Boolean.Parse(hdnId.Value);
                dayNight.Name = lblTime.Text;
                dayNight.FromTime = DateTime.ParseExact(Utils.StringCurrentDate() + Constants.SPACE + txtFrom.Text, Constants.DATE_TIME_PATTERN, new CultureInfo(Constants.CULTURE_VN));
                dayNight.ToTime = DateTime.ParseExact(Utils.StringCurrentDate() + Constants.SPACE + txtTo.Text, Constants.DATE_TIME_PATTERN, new CultureInfo(Constants.CULTURE_VN));
                dayNight.LastUser = emp.Id;

                //add list
                list.Add(dayNight);
            }

            if (dayNightBLL.Save(list))
            {
                Inits();
                lblMsg.Text = "Lưu thành công.";
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    } 
    #endregion

    #region Events
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        Save();
    }
    protected void btnReset_Click(object sender, ImageClickEventArgs e)
    {
        Page_Load(null, null);
    } 
    #endregion
}
